/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/PGLCircPolygonMarker.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

PGL_IMPLEMENT_DYNCREATE(CPGLCircPolygonMarker,CPGLMarker);

#ifdef _DEBUG
void CPGLCircPolygonMarker::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLMarker::Dump( dc );

    // now do the stuff for our specific class
	dc << _T("--- CPGLCircPolygonMarker") << GetID() <<_T(" ---\n");
}

void CPGLCircPolygonMarker::AssertValid() const
{
    // call inherited AssertValid first
    CPGLMarker::AssertValid();

	ASSERT(m_uSideCount >= 3);
	ASSERT(m_vX.size() == m_vY.size());
} 
#endif

void CPGLCircPolygonMarker::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLCircPolygonMarker");

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLMarker::SerializeXML(ar);

			xml.AddChildElemEx( _T("SideCount"), m_uSideCount);
			xml.AddChildElemEx( _T("AutoPhase"), m_bAutoPhase);
		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// loading base class
			CPGLMarker::SerializeXML(ar);

			xml.FindGetChildData( _T("SideCount"), m_uSideCount);
			xml.FindGetChildData( _T("AutoPhase"), m_bAutoPhase);
		xml.OutOfElem();

		NeedGeneration();
	}
}


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLCircPolygonMarker::CPGLCircPolygonMarker(const CPGLColor& _color, const CPGLColor& _FillColor,double dRadius, bool bFilled, UINT uSideCount)
: CPGLMarker(_color, _FillColor,dRadius,false), m_bNeedGeneration(true), m_bAutoPhase(true)
{
	SetSideCount( uSideCount );
}

CPGLCircPolygonMarker& CPGLCircPolygonMarker::operator = (const CPGLCircPolygonMarker& p)
{
	if (this != &p)
	{
		this->CPGLMarker::operator =( p);

		m_uSideCount=p.m_uSideCount;
		m_bAutoPhase = p.m_bAutoPhase;

		NeedGeneration();
	}

	return *this;
}

void CPGLCircPolygonMarker::Generate(bool bLegend)
{
	double angle = 2*PGL_PI / m_uSideCount;
	double radius;
	
	if (bLegend)
		radius=GetLegendRadius();
	else
		radius=GetRadius();

	m_vX.resize(m_uSideCount);
	m_vY.resize(m_uSideCount);

	if (m_bAutoPhase)
		AdjustPhase();

	for (UINT i=0;i<m_uSideCount;i++)
	{
		m_vX[i]=(float)(radius*cos( GetPhase() + angle*i));
		m_vY[i]=(float)(radius*sin( GetPhase() + angle*i));
	}


	m_bNeedGeneration=false;
}


void CPGLCircPolygonMarker::PlotGfx(gfxinterface::CGfxInterface& gfx, CPGLDataPtr pDataX, CPGLDataPtr pDataY)
{
	if (!pDataX || !pDataY || !IsVisible())
		return;

	const UINT n = __min( pDataX->size(), pDataY->size());

	if (n==0)
		return;

	if (IsGenerationNeeded())
		Generate();

	// drawing....
	CPGLMarker::PlotGfx(gfx, pDataX, pDataY);
	gfx.DrawPolygonStrip( *pDataX, *pDataY, m_vX, m_vY, GetLineType());
}

void CPGLCircPolygonMarker::PlotLegendGfx(gfxinterface::CGfxInterface& gfx, float x, float y)
{
	if (!IsVisible())
		return;

	Generate(true);
	m_bNeedGeneration=true;

	CPGLMarker::PlotLegendGfx(gfx, x, y);
	gfx.DrawPolygon( x, y, m_vX, m_vY, GetLineType());
}

CPGLCircPolygonMarkerPtr CPGLCircPolygonMarker::GetTriangle( double dRadius, bool bFilled )	
{	
	CPGLCircPolygonMarkerPtr p(new CPGLCircPolygonMarker(CPGLColor(),CPGLColor(1,1,1,1), dRadius, bFilled,3));

	p->SetName(_T("Triangle marker"));

	return p;
};

CPGLCircPolygonMarkerPtr CPGLCircPolygonMarker::GetSquare( double dRadius, bool bFilled )	
{	
	CPGLCircPolygonMarkerPtr p(new CPGLCircPolygonMarker(CPGLColor(),CPGLColor(1,1,1,1), dRadius, bFilled,4));

	p->SetName(_T("Square marker"));

	return p;
};

CPGLCircPolygonMarkerPtr CPGLCircPolygonMarker::GetPentagon( double dRadius, bool bFilled )	
{	
	CPGLCircPolygonMarkerPtr p(new CPGLCircPolygonMarker(CPGLColor(),CPGLColor(1,1,1,1), dRadius, bFilled,5));

	p->SetName(_T("Pentagon marker"));

	return p;
};

CPGLCircPolygonMarkerPtr CPGLCircPolygonMarker::GetHexagon( double dRadius, bool bFilled )	
{	
	CPGLCircPolygonMarkerPtr p(new CPGLCircPolygonMarker(CPGLColor(),CPGLColor(1,1,1,1), dRadius, bFilled,6));

	p->SetName(_T("Hexagon marker"));

	return p;
};
